package com.tuanbiao.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatSpinner;
import androidx.appcompat.widget.AppCompatTextView;

import com.tuanbiao.R;
import com.tuanbiao.constants.Constants;
import com.tuanbiao.utils.ClickUtil;

/**
 * @author abiao
 * @since 2022/4/20 10:33
 */
public class DetonatorSortDialog {
    public static final class Builder
            extends BaseDialog.Builder<CustomProgressDialog.Builder> {
        private Spinner spinnerSortType;
        private Spinner spinnerOrderType;
        private AppCompatTextView tvTitle;
        private AppCompatSpinner spOrderType;
        private AppCompatTextView tvRowDelay;
        private AppCompatSpinner spSortType;
        private AppCompatButton btnCancel;
        private AppCompatButton btnConfirm;
        private OnSortClickListener onSortClickListener;
        @Constants.DetonatorSortType
        private String sortType;
        @Constants.DetonatorOrderType
        private String orderType;

        public Builder(Context context) {
            super(context);
            setAnimStyle(BaseDialog.ANIM_TOAST);
            setBackgroundDimEnabled(true);
            setCancelable(false);

            setContentView(R.layout.dialog_detonator_sort);
            setGravity(Gravity.CENTER);


            tvTitle = (AppCompatTextView) findViewById(R.id.tv_title);
            spOrderType = (AppCompatSpinner) findViewById(R.id.sp_order_type);
            tvRowDelay = (AppCompatTextView) findViewById(R.id.tv_row_delay);
            spSortType = (AppCompatSpinner) findViewById(R.id.sp_sort_type);
            btnCancel = (AppCompatButton) findViewById(R.id.btn_cancel);
            btnConfirm = (AppCompatButton) findViewById(R.id.btn_confirm);

            btnConfirm.setOnClickListener(view -> {
                if (ClickUtil.isFastClick()) {
                    return;
                }
                if (null != onSortClickListener) {
                    onSortClickListener.onConfirm(sortType, orderType);
                }
                dismiss();
            });
            btnCancel.setOnClickListener(view -> dismiss());
            String[] sortData = {Constants.DetonatorSortTypes.ROW, Constants.DetonatorSortTypes.DELAY, Constants.DetonatorSortTypes.UID};
            spSortType.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.item_dialog_detonator_sort, sortData));
            spSortType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
                    sortType = sortData[position];
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                }
            });

            String[] orderData = {Constants.DetonatorOrderTypes.UP, Constants.DetonatorOrderTypes.DOWN};
            spOrderType.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.item_dialog_detonator_sort, orderData));
            spOrderType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
                    orderType = orderData[position];
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                    orderType = null;
                }
            });
        }

        public Builder setSortClickListener(OnSortClickListener onSortClickListener) {
            this.onSortClickListener = onSortClickListener;
            return this;
        }
    }

    public interface OnSortClickListener {
        default void onCancel() {

        }

        void onConfirm(@Constants.DetonatorSortType String sortType, @Constants.DetonatorOrderType String orderType);
    }
}
